<?php
/**
 * 质押记录
 */
namespace app\api\controller;

use app\api\model\BaseConfig as BaseConfigModel;
use app\common\model\MemberPledge as MemberPledgeModel;
use app\common\model\Currency as CurrencyModel;
use app\common\model\UserMoneyLog as UserMoneyLogModel;
use think\facade\Db;
use app\common\model\MemberPledgeLog as MemberPledgeLogModel;
use think\exception\ValidateException;

class Pledge extends Baek
{

    /**
     * 用户质押信息
     */
    public function info(){
        $info = MemberPledgeModel::memberPledge($this->mid);
        api_success($info);
    }

    /**
     * 参加质押
     */
    public function participate(){
        $money = $this->request->post('money', '', 'serach_in');
        $info = MemberPledgeModel::memberPledge($this->mid);
        if($money > 0){

            Db::startTrans();
            try{
                //获取质押信息
                $cid = BaseConfigModel::config_data('pledge_cid');
                $currency = CurrencyModel::getDetails($cid);

                $pay_statue = UserMoneyLogModel::change($this->mid,$currency['name'],-$money,'pay pledge','','参与质押'.$money);
                if(!$pay_statue){
                    // 回滚事务
                    Db::rollback();
                    api_error('','Sorry, your credit is running low',$this->language);
                }

                $info['money'] = bcadd($info['money'],$money,8);
                //更新质押
                MemberPledgeModel::where('id',$info['id'])->update(['money'=>$info['money']]);
                MemberPledgeLogModel::add($cid,$this->mid,$money,1);

                // 提交事务
                Db::commit();
                api_success();
            } catch (\Exception $e) {

                // 回滚事务
                Db::rollback();
                api_error('','Failed to submit, try again later!',$this->language);
            }

        }else{

            $info = MemberPledgeModel::memberPledge($this->mid);
            //判断金额
            if($info['money'] < -$money) api_error('','The pledge quantity has been exceeded',$this->language);

            Db::startTrans();
            try{
                $currency = CurrencyModel::getDetails($info['cid']);

                $info['money'] = bcadd($info['money'],$money,8);
                //更新质押
                MemberPledgeModel::where('id',$info['id'])->update(['money'=>$info['money']]);
                $pay_statue = UserMoneyLogModel::change($this->mid,$currency['name'],-$money,'Take out the pledge','','取出质押'.$money);
                if(!$pay_statue){
                    // 回滚事务
                    Db::rollback();
                    api_error('','Failed to submit, try again later!',$this->language);
                }
                MemberPledgeLogModel::add($info['cid'],$this->mid,-$money,2);

                // 提交事务
                Db::commit();
                api_success();
            } catch (\Exception $e) {

                // 回滚事务
                Db::rollback();
                api_error('','Failed to submit, try again later!',$this->language);
            }

        }
    }

    /**
     * 质押记录
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(){
        $page  = $this->request->post('page', 1, 'intval');  //矿机ID
        $limit  = $this->request->post('limit', 20, 'intval');  //购买数量
        $type = $this->request->post('type', 0, 'intval');  //购买数量

        //质押代币
        $pledge_cid = BaseConfigModel::config_data('pledge_cid');
        $where = ['mid'=>$this->mid,'cid'=>$pledge_cid];
        if($type == 1) $where['type'] = $type;
        if($type == 2) $where['type'] = $type;

        $list = MemberPledgeLogModel::getList($limit,$page,$where);

        api_success($list);
    }
}